package com.pangu.ui.refresh;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pangu.ui.R;


/**
 * 下拉刷新
 * Created by panxiaodong_91 on 2015/8/3.
 */
public class PullToRefreshView extends FrameLayout implements IPullableTracker {

    ImageView ground;
    ImageView rocket;
    ImageView moon;
    ImageView rocketShadow;
    ImageView refreshPrompt;
    ImageView flame;

    ImageView cloud1;
    ImageView cloud2;
    ImageView cloud3;

    TranslateAnimation liftOffAnimation;
    TranslateAnimation cloud1Animation;
    TranslateAnimation cloud2Animation;
    TranslateAnimation cloud3Animation;
    TranslateAnimation groundAnimation;
    TranslateAnimation moonAnimation;

    public PullToRefreshView(Context context) {
        super(context);

        initView(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.pull_refresh_view, null);
        ground = (ImageView) view.findViewById(R.id.ground);
        rocket = (ImageView) view.findViewById(R.id.rocket);
        flame = (ImageView) view.findViewById(R.id.flame);
        moon = (ImageView) view.findViewById(R.id.moon);
        rocketShadow = (ImageView) view.findViewById(R.id.rocket_shadow);
        refreshPrompt = (ImageView) view.findViewById(R.id.refresh_prompt);

        cloud1 = (ImageView) view.findViewById(R.id.cloud_1);
        cloud2 = (ImageView) view.findViewById(R.id.cloud_2);
        cloud3 = (ImageView) view.findViewById(R.id.cloud_3);

        addView(view);

        cloud1Animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        cloud1Animation.setRepeatCount(-1);
        cloud1Animation.setRepeatMode(Animation.RESTART);
        cloud1Animation.setDuration(3000);

        cloud2Animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        cloud2Animation.setRepeatCount(-1);
        cloud2Animation.setRepeatMode(Animation.RESTART);
        cloud2Animation.setDuration(1500);

        cloud3Animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        cloud3Animation.setRepeatCount(-1);
        cloud3Animation.setRepeatMode(Animation.RESTART);
        cloud3Animation.setDuration(2100);

        liftOffAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.15f, Animation.RELATIVE_TO_SELF, 0f);
        liftOffAnimation.setDuration(700);
        liftOffAnimation.setRepeatCount(-1);
        liftOffAnimation.setRepeatMode(Animation.REVERSE);
        liftOffAnimation.setInterpolator(new LinearInterpolator());

        groundAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        groundAnimation.setDuration(1000);
        groundAnimation.setFillAfter(true);

        moonAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.05f);
        moonAnimation.setDuration(1000);
        moonAnimation.setFillAfter(true);
    }

    @Override
    public void onRefreshInit() {
        rocket.clearAnimation();
        ground.clearAnimation();
        moon.clearAnimation();
        flame.setVisibility(GONE);
        int rocketMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
        RelativeLayout.LayoutParams rocketParams = (RelativeLayout.LayoutParams) rocket.getLayoutParams();
        rocketParams.bottomMargin = rocketMarginPx;
        rocket.setLayoutParams(rocketParams);
        rocket.requestLayout();
        int moonMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -30, getResources().getDisplayMetrics());
        RelativeLayout.LayoutParams moonParams = (RelativeLayout.LayoutParams) moon.getLayoutParams();
        moonParams.bottomMargin = moonMarginPx;
        moon.setLayoutParams(moonParams);
        moon.requestLayout();
        refreshPrompt.setVisibility(GONE);

        rocketShadow.setAlpha(0.0f);

        cloud1.setVisibility(GONE);
        cloud1.clearAnimation();
        cloud2.setVisibility(GONE);
        cloud2.clearAnimation();
        cloud3.setVisibility(GONE);
        cloud3.clearAnimation();
    }

    @Override
    public void onPullToRefresh(float refreshDis, float moveDis, ARefreshViewController controller) {
        float percentageChange = moveDis / refreshDis;
        int rocketMarginDp = (int) ((-80.0f * percentageChange) + 90.0f);
        if (rocketMarginDp <= 0) {
            rocketMarginDp = 10;
        }
        int rocketMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rocketMarginDp, getResources().getDisplayMetrics());
        RelativeLayout.LayoutParams rocketParams = (RelativeLayout.LayoutParams) rocket.getLayoutParams();
        rocketParams.bottomMargin = rocketMarginPx;
        rocket.setLayoutParams(rocketParams);
        rocket.requestLayout();

        int moonMarginDp = (int) ((40.0f * percentageChange) -30.0f);
        int moonMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, moonMarginDp, getResources().getDisplayMetrics());
        RelativeLayout.LayoutParams moonParams = (RelativeLayout.LayoutParams) moon.getLayoutParams();
        moonParams.bottomMargin = moonMarginPx;
        moon.setLayoutParams(moonParams);
        moon.requestLayout();

        rocketShadow.setAlpha(percentageChange);
    }

    @Override
    public void onReleaseToRefresh() {
        refreshPrompt.setVisibility(VISIBLE);
        rocket.startAnimation(liftOffAnimation);
    }

    @Override
    public void onRefreshing() {
        refreshPrompt.setVisibility(GONE);
        flame.setVisibility(VISIBLE);
        rocket.clearAnimation();
        ground.startAnimation(groundAnimation);
        rocketShadow.setAlpha(0.0f);

        cloud1.setVisibility(VISIBLE);
        cloud1.startAnimation(cloud1Animation);
        cloud2.setVisibility(VISIBLE);
        cloud2.startAnimation(cloud2Animation);
        cloud3.setVisibility(VISIBLE);
        cloud3.startAnimation(cloud3Animation);

        moon.startAnimation(moonAnimation);
    }

    @Override
    public void onRefreshSuccess() {

    }

    @Override
    public void onRefreshError() {

    }
}
